package com.peakmain.wanandroid.activity;

import android.graphics.Bitmap;
import android.os.Environment;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.peakmain.baselibrary.utils.ImageUtils;
import com.peakmain.homemodule.HomeFragment;
import com.peakmain.systemmoudle.SystemFragment;
import com.peakmain.wanandroid.R;
import com.peakmain.wanandroid.mymodule.MineFragment;
import com.peakmain.wanandroid.netmodule.base.BaseActivity;
import com.peakmain.wanandroid.projectmodule.ProjectFragment;

import butterknife.BindView;

@Route(path = "/activity/MainActivity")
public class MainActivity extends BaseActivity {
    @BindView(R.id.bottom_navigation)
    BottomNavigationView mBottomNavigation;
    private static final int FRAGMENT_HOME = 0;
    private static final int FRAGMENT_PROJECT = 1;
    private static final int FRAGMENT_SYSTEM = 2;
    private static final int FRAGMENT_ME = 3;
    private HomeFragment mHomeFragment;
    private ProjectFragment mProjectFragment;
    private SystemFragment mSystemFragment;
    private MineFragment mMineFragment;
    private int position;//当前选中的位置

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initInjector() {
        ARouter.getInstance().inject(this);
    }

    @Override
    protected void initView() {


        showFragment(FRAGMENT_HOME);
        mBottomNavigation.setOnNavigationItemSelectedListener(this::onOptionsItemSelected);
        mBottomNavigation.setSelectedItemId(R.id.menu_home);
        String path = Environment.getExternalStorageDirectory() +"/三级证书.jpg";
        Bitmap bitmap = ImageUtils.getInstance().decodeFile(path);

        ImageUtils.getInstance().compressImage(bitmap, 90, path);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.menu_home:
                showFragment(FRAGMENT_HOME);
                return true;
            case R.id.menu_project:
                showFragment(FRAGMENT_PROJECT);
                return true;
            case R.id.menu_system:
                showFragment(FRAGMENT_SYSTEM);
                return true;
            case R.id.menu_me:
                showFragment(FRAGMENT_ME);
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showFragment(int index) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hintFragment(ft);
        position = index;
        switch (index) {
            case FRAGMENT_HOME://新闻
                /**
                 * 如果Fragment为空，就新建一个实例
                 * 如果不为空，就将它从栈中显示出来
                 */
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    ft.add(R.id.container, mHomeFragment, HomeFragment.class.getName());
                } else {
                    ft.show(mHomeFragment);
                }
                break;
            case FRAGMENT_PROJECT:
                if (mProjectFragment == null) {
                    mProjectFragment = new ProjectFragment();
                    ft.add(R.id.container, mProjectFragment, ProjectFragment.class.getName());
                } else {
                    ft.show(mProjectFragment);
                }
                break;
            case FRAGMENT_SYSTEM:
                if (mSystemFragment == null) {
                    mSystemFragment = new SystemFragment();
                    ft.add(R.id.container, mSystemFragment, SystemFragment.class.getName());
                } else {
                    ft.show(mSystemFragment);
                }
                break;
            case FRAGMENT_ME:
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    ft.add(R.id.container, mMineFragment, MineFragment.class.getName());
                } else {
                    ft.show(mMineFragment);
                }
                break;
            default:
                break;
        }
        ft.commit();
    }

    /**
     * 隐藏fragment
     *
     * @param ft
     */
    private void hintFragment(FragmentTransaction ft) {
        // 如果不为空，就先隐藏起来
        if (mHomeFragment != null) {
            ft.hide(mHomeFragment);
        }
        if (mProjectFragment != null) {
            ft.hide(mProjectFragment);
        }
        if (mSystemFragment != null) {
            ft.hide(mSystemFragment);
        }
        if (mMineFragment != null) {
            ft.hide(mMineFragment);
        }
    }
}
